package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.advanced;


import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelProxyFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server.AbstractHandleSocksReportClientProxyServerTransferRequestTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelFlowEnum;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProtocolType;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProxyType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.proxy.ServerChannelProxyFlow;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler.NettySocksClientProxyServerRealHandler;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
import org.wu.framework.core.utils.ObjectUtils;


@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleSocksReportClientProxyServerTransferRequestTypeAdvanced
        extends AbstractHandleSocksReportClientProxyServerTransferRequestTypeAdvanced<NettyProxyMsg> {


    private final ChannelProxyFlowAdapter channelProxyFlowAdapter;

    public ServerHandleSocksReportClientProxyServerTransferRequestTypeAdvanced(ChannelProxyFlowAdapter channelProxyFlowAdapter) {
        this.channelProxyFlowAdapter = channelProxyFlowAdapter;
    }

    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     * @see NettySocksClientProxyServerRealHandler
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
        Channel transferChannel = nettyChannelContext.channel();

        String targetIp = ChannelAttributeKeyUtils.getTargetIp(transferChannel);
        Integer targetPort = ChannelAttributeKeyUtils.getTargetPort(transferChannel);
        String clientId = ChannelAttributeKeyUtils.getClientId(transferChannel);
        // 目标通道
        Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(transferChannel);
        // 目标数据发送
        if (ObjectUtils.isNotEmpty(nextChannel) && nextChannel.isActive()) {
            ByteBuf buf = nextChannel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
            buf.writeBytes(nettyProxyMsg.getData());
            nextChannel.writeAndFlush(buf);

            // 记录进口数据
            ServerChannelProxyFlow serverChannelFlow = ServerChannelProxyFlow
                    .builder()
                    .channelFlowEnum(ChannelFlowEnum.IN_FLOW)
                    .port(targetPort)
                    .ip(targetIp)
                    .clientId(clientId)
                    .channelProxyType(ChannelProxyType.CLIENT_PROXY_SEVER)
                    .channelProtocolType(ChannelProtocolType.SOCKS)
                    .flow(nettyProxyMsg.getData().length)
                    .build();
            channelProxyFlowAdapter.asyncHandler(transferChannel, serverChannelFlow);
        } else {
            log.error("当前目标通道已经关闭或者不存在");
        }

    }

}
